'use strict';

/**
 * Created by zyf on 16-2-16.
 */

angular.module('metalabPartnerApp').config(['$stateProvider', function ($stateProvider) {

    var messagePromise = function (tag) {
        return ['$rootScope', 'MetaResources', '$q', function ($rootScope, MetaResources, $q) {

            //异步，需要保证是在token获取之后发送的_self请求.
            var defer = $q.defer();
            $rootScope.passport.promise.then(function () {
                MetaResources.message.query({userId: '_self', '_range': 0, 'tag': tag}, function (data, headers) {
                    defer.resolve(headers()['x-count']);
                }, function (error) {
                    defer.reject(error);
                });
            });

            return defer.promise;
        }];
    };

    $stateProvider.state('meta.message', {
        abstract: true,
        url: '/message?page',
        params: {
            page: '1'
        },
        resolve: {
            allTotal: messagePromise(),
            systemTotal: messagePromise('系统消息'),
            personalTotal: messagePromise('个人动态'),
            projectTotal: messagePromise('项目进展'),
            recommendTotal: messagePromise('项目推荐'),
            accountTotal: messagePromise('账户消息')
        },
        views: {
            'body@meta': {
                templateUrl: 'views/message/index.html',
                controller: 'MessageIndexCtrl'
            }
        }
    }).state('meta.message.all', {
        url: '/all',
        views: {
            'message': {
                templateUrl: 'views/message/template/message.html',
                controller: 'MessageCtrl'
            }
        },
        data: {
            tag: undefined
        }
    }).state('meta.message.system', {
        url: '/system',
        views: {
            'message': {
                templateUrl: 'views/message/template/message.html',
                controller: 'MessageCtrl'
            }
        },
        data: {
            tag: '系统消息'
        }
    }).state('meta.message.personal', {
        url: '/personal',
        views: {
            'message': {
                templateUrl: 'views/message/template/message.html',
                controller: 'MessageCtrl'
            }
        },
        data: {
            tag: '个人动态'
        }
    }).state('meta.message.project', {
        url: '/project',
        views: {
            'message': {
                templateUrl: 'views/message/template/message.html',
                controller: 'MessageCtrl'
            }
        },
        data: {
            tag: '项目进展'
        }
    }).state('meta.message.recommend', {
        url: '/recommend',
        views: {
            'message': {
                templateUrl: 'views/message/template/message.html',
                controller: 'MessageCtrl'
            }
        },
        data: {
            tag: '项目推荐'
        }
    }).state('meta.message.account', {
        url: '/account',
        views: {
            'message': {
                templateUrl: 'views/message/template/message.html',
                controller: 'MessageCtrl'
            }
        },
        data: {
            tag: '账户消息'
        }
    });
}]);
